home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11685 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Need to get value from 2-D matrix
  5. Date: 25 Mar 1996 20:10:59 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4j6ukj$pp7@sparcserver.lrz-muenchen.de>
  9. References: <DotDpo.6o2@news.uwindsor.ca>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. kima@uwindsor.ca (Sam Kim) writes:
  13.  
  14.  
  15. >I'm writing a program that reads in a 2-D matrix from binary.  I am
  16. >useless with structures, so I'm trying to do this with arrays and
  17. >pointers.
  18.  
  19. >Question :  How do I read in the values?
  20.  
  21. >I have : 
  22.  
  23. >for (i=0; i<n; i++)
  24. >  for (j=0; j<n; j++)
  25. >    {
  26. >      fread(&ptr,1,1,fp)
  27.  
  28. From this we can derive that
  29.  
  30.  a) "matrix" is a matrix of char (or any other quantity with the same size)
  31.  b) "ptr" is a char (or ...)
  32.  
  33. >      matrix[i][j] = ptr;
  34.  
  35. Is there a _real_ need for the intermediate variable "ptr". What prevents
  36.  
  37.    fread(&matrix[i][j], 1, 1, fp);
  38.  
  39. Is there a _real_ need for reading the elements of the _inner_ array
  40. one at a time? Why not
  41.  
  42.    fread(matrix[i], 1, n, fp);
  43.  
  44. This should work in all situations that allow you to use a double
  45. subscript.
  46.  
  47. >      ptr++;
  48.  
  49. Is there a specific reason to increment an intermediate variable at
  50. this point? The next call to fread() will overwrite the value in ptr
  51. anyway.
  52.  
  53. >    }
  54.  
  55. >Shouldn't this work?
  56.  
  57. Yes, in fact it might work, given that my assumptions based on the
  58. code you showed us are true.
  59.  
  60. Kurt
  61. --
  62. | Kurt Watzka                             Phone : +49-89-2180-6254
  63. | watzka@stat.uni-muenchen.de
  64. | ua302aa@sunmail.lrz-muenchen.de
  65.  
  66.